//程序名qsort.c  使用:./qsort 文件大小(M) 文件名
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/time.h>
#define BLOCK 1024*1024
float *data;

int float_cmp(const void *a,const void *b)
{  
   	float*_a = (float*)a;
	float*_b = (float*)b;
	if (*_a > *_b) {
		return -1;
	}
	else if (*_a < *_b) {
		return 1;
	}
	else {
		return 0;
	}
}

  int main(int argc,char* argv[])
{
  uint64_t loop;
  uint64_t n;
  int i,j;
  struct timeval tv_start,tv_end;
  long start,end;
  double ste;
  if(argc!=3)
  {
     printf("Input:%s datasize(M) data_file\n",argv[0]);
     return -1;
  }   
  loop=atol(argv[1]);
  FILE * fp;
  fp=fopen(argv[2],"r");
  if(fp==NULL)
 {
   printf("Cannot open file %s\n",argv[2]);
   return -1; 
 }  
  data=(float *)malloc(sizeof(float)*BLOCK*loop);
  if (!data) {
    printf("malloc failed\n");
    return -1;
  }
  n=BLOCK*loop;
  fread(data,sizeof(float),n,fp);
  gettimeofday(&tv_start,NULL);
  qsort(data,n,sizeof(float),float_cmp);
  gettimeofday(&tv_end,NULL);
  ste=(tv_end.tv_sec-tv_start.tv_sec)+((double)(tv_end.tv_usec-tv_start.tv_usec))/1000000;
  printf("use time %lfs\n",ste);
  free(data);
  fclose(fp);
}
